//
//  FamousViewController.m
//  Beauty
//
//  Created by Snow on 16/8/29.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "FamousViewController.h"
#import "GXMacro.h"
#import "GXNetWorkingParser.h"
#import "HappyGivenTableViewCell.h"
#import "HeadCollectionViewOfHappyGiven.h"
#import "ModelOfHead.h"
#import "ModelOfHappyGiven.h"
#import "UIView+Frame.h"
#import "NewProductViewController.h"
@interface FamousViewController ()<UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource, SectionDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arrayOfHeader;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *arrayOfSection;

@end

@implementation FamousViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
    UILabel *label = [[UILabel alloc] initWithFrame:view.frame];
    label.text = @"让送礼变得更有趣";
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:18];
    [view addSubview:label];
    label.textAlignment = 1;
    self.navigationItem.titleView = view;
    [self handleData];
    [self createView];
    [self createFresh];
}
- (void)createFresh
{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
    }];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(handleLoadNewData)];
}
- (void)handleLoadNewData {
    //请求服务器更新数据
    _arrayOfHeader = [NSMutableArray array];
    [GXNetWorkingParser GETWithURL:@"http://www.wantease.com/mobile1/index.php?act=happy_send&op=commend_list" par:nil success:^(id responseObject) {
        NSArray *arr = [responseObject objectForKey:@"datas"];
        for (NSDictionary *dic in arr) {
            ModelOfHead *model = [[ModelOfHead alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_arrayOfHeader addObject:model];
        }
        [_collectionView reloadData];
    } filed:^(NSError *error) {
        
    }];
    [self.tableView.mj_header endRefreshing];
}
- (void)handleData
{
    _arrayOfSection = [NSMutableArray array];
    _arrayOfHeader = [NSMutableArray array];
    [GXNetWorkingParser GETWithURL:@"http://www.wantease.com/mobile1/index.php?act=happy_send&op=commend_list" par:nil success:^(id responseObject) {
    NSArray *arr = [responseObject objectForKey:@"datas"];
    for (NSDictionary *dic in arr) {
        ModelOfHead *model = [[ModelOfHead alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [_arrayOfHeader addObject:model];
    }
        [_collectionView reloadData];
} filed:^(NSError *error) {
    
}];
}

- (void)createView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.view.bounds.size.height - 49 - 64) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[HappyGivenTableViewCell class] forCellReuseIdentifier:@"HappyGivenTableViewCell"];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 400)];
    view.backgroundColor = [UIColor colorWithRed:0.329 green:0.357 blue:0.427 alpha:1.000];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(SCREEN_WIDTH - 100, 300);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[HeadCollectionViewOfHappyGiven class] forCellWithReuseIdentifier:@"HeadCollectionViewOfHappyGiven"];
    _collectionView.backgroundColor = [UIColor colorWithRed:0.329 green:0.357 blue:0.427 alpha:1.000];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [view addSubview:button];
    button.frame = CGRectMake(0, 315, 50, 25);
    button.x_conterX = self.view.x_conterX;
    [button setImage:[UIImage imageNamed:@"buttonOfFile"] forState:UIControlStateNormal];
    UIButton *buttonOfLeft = [UIButton buttonWithType:UIButtonTypeCustom];
    [view addSubview:buttonOfLeft];
    buttonOfLeft.frame = CGRectMake(80, 315, 50, 25);
    [buttonOfLeft setImage:[UIImage imageNamed:@"buttonOfLike"] forState:UIControlStateNormal];
    UIButton *buttonOfRight = [UIButton buttonWithType:UIButtonTypeCustom];
    [view addSubview:buttonOfRight];
    buttonOfRight.frame = CGRectMake(245, 315, 50, 25);
    [view addSubview:self.collectionView];
    [buttonOfRight setImage:[UIImage imageNamed:@"buttonOfShare"] forState:UIControlStateNormal];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 350, SCREEN_WIDTH, 50)];
    label.text = @"匹配";
    label.backgroundColor = [UIColor whiteColor];
    [view addSubview:label];
    UIButton *buttonOfSort = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonOfSort.frame = CGRectMake(SCREEN_WIDTH - 50, 360, 30, 30);
    [buttonOfSort setBackgroundImage:[UIImage imageNamed:@"buttonOfSort"] forState:UIControlStateNormal];
    [view addSubview:buttonOfSort];
    _tableView.tableHeaderView = view;

}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _arrayOfHeader.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HeadCollectionViewOfHappyGiven *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HeadCollectionViewOfHappyGiven" forIndexPath:indexPath];
    ModelOfHead *model = [_arrayOfHeader objectAtIndex:indexPath.row];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:model.goods_image]];
    cell.labelOfPrice.text = [NSString stringWithFormat:@" ¥ %@", model.goods_price];
    cell.labelOfContent.text = model.goods_name;
    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HappyGivenTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HappyGivenTableViewCell"];
    cell.delegate = self;
    cell.contentView.backgroundColor = [UIColor colorWithWhite:0.808 alpha:1.000];
    return cell;
}
- (void)giveSectionArray:(NSArray *)array
{
    _arrayOfSection = array.mutableCopy;
    [_tableView reloadData];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 450;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 350;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 350)];
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 280)];
    UILabel *labelOfName = [[UILabel alloc] initWithFrame:CGRectMake(0, 285, SCREEN_WIDTH, 30)];
    UILabel *labelOfContent = [[UILabel alloc] initWithFrame:CGRectMake(0, 315, SCREEN_WIDTH, 30)];
    [view addSubview:image];
    [view addSubview:labelOfName];
    [view addSubview:labelOfContent];
    ModelOfHappyGiven *model = [_arrayOfSection objectAtIndex:section];
    [image sd_setImageWithURL:[NSURL URLWithString:model.goods_image]];
    if (model.goods_name.length < 8) {
        
        labelOfName.text = model.goods_name;
    } else {
        labelOfName.text = [model.goods_name substringFromIndex:8];
    }
    labelOfContent.text =[NSString stringWithFormat:@"¥ %@", model.goods_price];
    labelOfName.textAlignment = 1;
    labelOfContent.textAlignment = 1;
    UITapGestureRecognizer *tapHeaderView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleHeader:)];
    [view addGestureRecognizer:tapHeaderView];
    return view;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ModelOfHead *model = [_arrayOfHeader objectAtIndex:indexPath.row];
    NewProductViewController *new = [[NewProductViewController alloc] init];
    new.goods_id = model.goods_id;
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:new animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}
- (void)happyGivenWithGood_id:(NSString *)good_id
{
    NewProductViewController *new = [[NewProductViewController alloc] init];
    new.goods_id = good_id;
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:new animated:YES];
}
- (void)handleHeader:(UIGestureRecognizer *)tap
{
    ModelOfHead *model = [_arrayOfSection objectAtIndex:0];
    NewProductViewController *new = [[NewProductViewController alloc] init];
    new.goods_id = model.goods_id;
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:new animated:YES];
}
#pragma mark - Navigation

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
 CGFloat sectionHeaderHeight = 400; //这里是我的headerView和footerView的高度
 if (_tableView.contentOffset.y<=sectionHeaderHeight&&_tableView.contentOffset.y>=0) {
 _tableView.contentInset = UIEdgeInsetsMake(-_tableView.contentOffset.y, 0, 0, 0);
 } else if (_tableView.contentOffset.y>=sectionHeaderHeight) {
 _tableView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
 }
}


@end
